#!/bin/sh
renice -10 $$
ENABLED="yes"
WORK_DIR="/opt/etc/AdGuardHome"
LOG_FILE="syslog"
PID_FILE="/opt/var/run/AdGuardHome.pid"
PROCS="AdGuardHome"
GOGC="40"
GOMAXPROCS="$(grep -c "processor" /proc/cpuinfo)"
GOMAXPROCS="$((GOMAXPROCS == 4 ? 2 : (GOMAXPROCS == 2 ? 1 : 1)))"
GOMEMLIMIT="$(free | awk -v VAR=$GOMAXPROCS '/Mem/{print int(($4*0.9537/1024)/VAR)}')MiB"
ARGS="-s run -c ${WORK_DIR}/AdGuardHome.yaml -w $WORK_DIR --pidfile $PID_FILE --no-check-update -l $LOG_FILE"
PREARGS="env TZ=/etc/localtime GOGC=${GOGC} GOMAXPROCS=${GOMAXPROCS} GOMEMLIMIT=${GOMEMLIMIT} QUIC_GO_DISABLE_ECN=true"
PRECMD="{ ulimit -v 1750000; ulimit -m 450000; ulimit -s 8192; } && { if pidof syslog-ng logrotate | wc -w | grep -qE '^[^0]$'; then scribe restart; fi; until pidof $PROCS | grep -qE '.*'; do { killall -9 $(netstat -nlp 2>/dev/null | awk -F/ -v VAR=$PROCS '$0 ~ /.*:53[[:space:]].*/ && $0 !~ VAR {if(!x[$2]++) printf "%s ", $2}') || kill -s 9 $(netstat -nlp 2>/dev/null | awk -v VAR=$PROCS '$0 ~ /.*:53[[:space:]].*/ && $0 !~ VAR {match($7, /[0-9]+/); if (RSTART && !x[$7]++) printf "%s ", substr($7, RSTART, RLENGTH)}'); }; service stop_dnsmasq; done; } &"
POSTCMD="service restart_dnsmasq"
DESC="$PROCS"
PATH="/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export TZ="$(cat /etc/TZ)"
[ -z "$SCRIPT_LOC" ] && . /jffs/addons/AdGuardHome.d/AdGuardHome.sh
